package com.iot.inter.controller.newinterface.query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.responseBody.QueryOrderResponseBody;
import com.iot.inter.entity.newpojo.response.subResponse.common.PackageOrder;
import com.iot.inter.entity.newrequest.queryorder.request.RequestordersQueryNew;
import com.iot.inter.entity.newrequest.queryorder.response.ResponseordersQueryNew;
import com.iot.inter.entity.newrequest.queryorder.response.ResponseordersQueryTradeData;
import com.iot.inter.interfaceprocessing.newinterface.TrunToQueryorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.16查询套餐订单信息：1.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.16查询套餐订单信息")
public class QueryPackageOrderNewController {

    private static Logger logger = Logger.getLogger(QueryPackageOrderNewController.class);

    @CrossOrigin
    @RequestMapping(value = "queryPackageOrderNew", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.16查询套餐订单信息", response = ResponseordersQueryNew.class)
    public String query(@ApiParam @RequestBody RequestordersQueryNew request) {
        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {
            logger.info("【无需查首单：查询套餐订单】请求：" + requestStr);

            RequestordersQueryNew requestordersQuery = JSONArray.parseObject(requestStr, RequestordersQueryNew.class);
            String orderType = requestordersQuery.getOrderType();
            String deviceId = requestordersQuery.getDeviceId();
            String orderId = requestordersQuery.getOrderId();
            String orderStatus = requestordersQuery.getOrderStatus();
            String secret = requestordersQuery.getSecret();
            String partnerCode = requestordersQuery.getPartnerCode();

            TrunToQueryorderService queryorderService = new TrunToQueryorderService();
            QueryOrderResponseBody queryOrderResponseBody = queryorderService.queryOrder(partnerCode, deviceId, orderType, orderId, orderStatus, secret);

            List<ResponseordersQueryTradeData> tradeDataList = new ArrayList<ResponseordersQueryTradeData>();

            if (queryOrderResponseBody != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(queryOrderResponseBody.getRetCode())) {

                    if (queryOrderResponseBody.getPackageOrder() != null) {

                        List<PackageOrder> packageOrderList = queryOrderResponseBody.getPackageOrder();

                        if (packageOrderList != null && packageOrderList.size() > 0 && !packageOrderList.isEmpty()) {
                            //从body中取出多个Package_order

                            for (PackageOrder order : packageOrderList) {
                                //没有加油包
                                ResponseordersQueryTradeData tra = new ResponseordersQueryTradeData();
                                tra.setOrderId(order.getOrderCode());
                                tra.setDeviceId(order.getDeviceId());
                                tra.setPackageCode(order.getPackageCode());
                                tra.setPackageName(order.getPackageName());
                                tra.setPackageType(order.getPackageType());
                                tra.setOrderStatus(order.getOrderStatus());
                                tra.setBuyType(order.getBuyType());
                                tra.setOrderPeriod(order.getOrderPeriod());
                                tra.setFlow(order.getFlow());
                                tra.setUsedFlow(order.getUsedFlow());
                                tra.setFlowUseRate(order.getFlowUseRate());
                                tra.setOrderUsedFlow(order.getOrderUsedFlow());
                                tra.setOrderDate(order.getOrderDate());
                                tra.setActiveDate(order.getActiveDate());
                                tra.setExpireDate(order.getExpireDate());
                                tra.setIsOrderRefuelingPackage(order.getIsOrderRefuelingPackage());
                                tradeDataList.add(tra);
                            }

                            ResponseordersQueryNew response = new ResponseordersQueryNew();
                            response.setTradeData(tradeDataList);
                            response.setRetCode("0000");
                            response.setResultInfo("成功");
                            // 返回给调用者的报文
                            jsonString = JSON.toJSONString(response);
                            logger.info("【无需查首单：查询套餐订单】成功响应：" + jsonString);
                            return jsonString;

                        } else {
                            ResponseordersQueryNew response = new ResponseordersQueryNew();
                            response.setTradeData(tradeDataList);
                            response.setRetCode(queryOrderResponseBody.getRetCode());
                            response.setResultInfo(queryOrderResponseBody.getResultInfo());
                            // 返回给调用者的报文
                            jsonString = JSON.toJSONString(response);
                            logger.info("【无需查首单：查询套餐订单】响应：" + jsonString);
                            return jsonString;
                        }

                    } else {
                        ResponseordersQueryNew response = new ResponseordersQueryNew();
                        response.setRetCode(queryOrderResponseBody.getRetCode());
                        response.setTradeData(tradeDataList);
                        response.setResultInfo(queryOrderResponseBody.getResultInfo());
                        // 返回给调用者的报文
                        jsonString = JSON.toJSONString(response);
                        logger.info("【无需查首单：查询套餐订单】响应：" + jsonString);
                        return jsonString;

                    }

                } else {

                    ResponseordersQueryNew response = new ResponseordersQueryNew();
                    response.setRetCode(queryOrderResponseBody.getRetCode());
                    response.setResultInfo(queryOrderResponseBody.getResultInfo());
                    response.setTradeData(tradeDataList);
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(response);
                    logger.info("【无需查首单：查询套餐订单】响应：" + jsonString);
                    return jsonString;


                }

            } else {
                ResponseordersQueryNew response = new ResponseordersQueryNew();
                response.setTradeData(tradeDataList);
                response.setRetCode("9998");
                response.setResultInfo("接口返回有误");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(response);
                logger.info("【无需查首单：查询套餐订单】响应：" + jsonString);
                return jsonString;
            }
        } else {
            ResponseordersQueryNew response = new ResponseordersQueryNew();
            response.setRetCode("9998");
            response.setResultInfo("请求为空!");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(response);
            logger.info("【无需查首单：查询套餐订单】响应：" + jsonString);
            return jsonString;

        }

    }

}
